<template>
    <view v-if="adSwitchKg" :class="homeADList.type == 8 ? '' : 'adview-box'">
        <view
            class="cu-card case no-card margin-top margin-bottom ad-box"
            v-if="
                centerSwicth &&
                    adSwitchKg &&
                    homeADList &&
                    JSON.stringify(homeADList) != '{}' &&
                    homeADList.ad_type < 3 &&
                    homeADList.type != 8
            "
            @click="goaddetail()"
        >
            <view class="cu-item shadow">
                <view class="image">
                    <image
                        v-if="homeADList.ad_type == 1"
                        style="max-height: 220upx;"
                        :src="
                            homeADList.img.search('http') >= 0
                                ? homeADList.img
                                : apiImageUrl + homeADList.img
                        "
                    ></image>
                    <video
                        id="adVideo"
                        :src="homeADList.v_url"
                        style="max-height: 220upx;width: 100%;"
                        :autoplay="true"
                        loop
                        muted
                        :show-play-btn="false"
                        :controls="false"
                        objectFit="fill"
                        v-if="homeADList.ad_type == 2"
                    ></video>
                </view>
            </view>
        </view>

        <view
            class="cu-card case no-card ad-box"
            v-if="
                centerSwicth &&
                    adSwitchKg &&
                    homeADList &&
                    JSON.stringify(homeADList) != '{}' &&
                    homeADList.ad_type < 3 &&
                    homeADList.type == 8
            "
            @click="goaddetail()"
        >
            <image
                style="max-height: 240upx;width: 100%;"
                :src="
                    homeADList.img.search('http') >= 0
                        ? homeADList.img
                        : apiImageUrl + homeADList.img
                "
            ></image>
        </view>

        <view
            class="cu-card case no-card ad-box"
            v-if="
                centerSwicth &&
                    adSwitchKg &&
                    homeADList &&
                    JSON.stringify(homeADList) != '{}' &&
                    homeADList.ad_type >= 3
            "
        >
            <!-- #ifdef MP-WEIXIN -->
            <!-- 小程序banner广告 -->
            <ad
                v-if="homeADList.ad_type == 3"
                :unit-id="homeADList.url"
                ad-intervals="35"
            ></ad>
            <!-- 小程序视频广告 -->
            <ad
                v-if="homeADList.ad_type == 4"
                ad-type="video"
                :unit-id="homeADList.url"
                ad-intervals="35"
            ></ad>
            <!-- 小程序原生模版广告 -->
            <ad-custom
                v-if="homeADList.ad_type == 9"
                :unit-id="homeADList.url"
                ad-intervals="35"
            ></ad-custom>
            <!-- #endif -->
        </view>
    </view>
</template>

<script>
const MyApp = getApp();
const globalData = MyApp.globalData;
export default {
    props: {
        adSwitchKg: Number,
        homeADList: {
            type: [Array, Object, String],
            default: null
        }
    },
    data() {
        return {
            num: {
                days: 1,
                wacth: 1,
                good: 1,
                feedback: 1
            },
            imageUrl: this.$config.imageUrl,
            apiImageUrl: this.$config.apiImageUrl,
            videoContext: null,
            pageMpadStyle: '',
            centerSwicth: 1
        };
    },
    mounted() {
        this.init();
    },
    watch: {
        homeADList(newval) {
            // console.log(newval);
        }
    },
    computed: {
        i18n() {
            return globalData.$t('common');
        }
    },
    methods: {
        init() {
            this.num.days = this.randomNum(1, 31);
            this.num.wacth = this.randomNum(0, 100);
            this.num.good = this.randomNum(0, 100);
            this.num.feedback = this.randomNum(0, 100);
            let sysconfig = uni.getStorageSync('sys_config');
            // 会员无广告
            if (
                sysconfig &&
                sysconfig.no_ad_limit > 0 &&
                sysconfig.userinfo && sysconfig.no_ad_limit_arr &&
				sysconfig.no_ad_limit_arr.indexOf(sysconfig.userinfo.group_id) != -1
            ) {
                this.centerSwicth = 0;
            }
        },
        goaddetail() {
            // #ifdef H5
            window.open(this.homeADList.url);
            // #endif
            // #ifdef APP-PLUS
            plus.runtime.openWeb(this.homeADList.url);
            // #endif
            // #ifdef MP-WEIXIN
            uni.navigateTo({
                url:
                    '/pages/webview/about?url=' +
                    encodeURIComponent(this.homeADList.url)
            });
            // #endif
        },
        randomNum(minNum, maxNum) {
            return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
        }
    }
};
</script>

<style>
.adview-box {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
    align-content: center;
    padding: 10upx 0;
    /* width: 750upx; */
}
.ad-box {
    width: 100%;
}
</style>
